package com.liser.southernstar.collection.ui.adapter;


import android.graphics.Color;
import android.graphics.Typeface;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.liser.southernstar.R;
import com.liser.southernstar.collection.model.vo.OrderTypeVO;

import java.util.HashMap;
import java.util.List;

public class OrderTypeAdapter extends BaseQuickAdapter<OrderTypeVO, BaseViewHolder> {

    private int checked;

    public boolean fromClick;

    private String typeStr;

    private HashMap<String, Long> badges = new HashMap<>();

    public OrderTypeAdapter() {
        super(R.layout.adapter_order_type);
    }

    public OrderTypeAdapter(@Nullable List<OrderTypeVO> data) {
        super(R.layout.adapter_order_type, data);
        if (data != null && data.size() > 0) {
            typeStr = data.get(0).getName();
        }
    }

    public void updateBadge(HashMap<String, Long> badges) {
        this.badges = badges;
        notifyDataSetChanged();
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderTypeVO orderTypeVO) {
        helper.setText(R.id.tv_name, orderTypeVO.getName())
                .setTag(R.id.item_main, orderTypeVO.getId());
        if (helper.getAdapterPosition() == checked) {
            helper.setBackgroundColor(R.id.item_main, Color.WHITE)
                    .setTextColor(R.id.tv_name, Color.BLACK)
                    .setTypeface(R.id.tv_name, Typeface.DEFAULT_BOLD);
        } else {
            helper.setBackgroundColor(R.id.item_main, ContextCompat.getColor(mContext, R.color.type_gray))
                    .setTextColor(R.id.tv_name, ContextCompat.getColor(mContext, R.color.type_normal))
                    .setTypeface(R.id.tv_name, Typeface.DEFAULT);
        }
        Long afterCount = badges.get(orderTypeVO.getId());
        if (badges.containsKey(orderTypeVO.getId()) && (afterCount == null ? 0 : afterCount) > 0) {
            helper.setVisible(R.id.item_badge, true)
                    .setText(R.id.item_badge, String.valueOf(badges.get(orderTypeVO.getId())));
        } else {
            helper.setVisible(R.id.item_badge, false);
        }
    }

    public void setChecked(int checked) {
        this.checked = checked;
        typeStr = getData().get(checked).getName();
        notifyDataSetChanged();
    }

    public void setType(String type) {
        if (fromClick) {
            fromClick = !type.equals(typeStr);
            return;
        }
        if (type.equals(typeStr)) {
            return;
        }
        for (int i = 0; i < getData().size(); i++) {
            if (getData().get(i).getName().equals(type) && i != checked) {
                setChecked(i);
                moveToPosition(i);
                break;
            }
        }
    }

    private void moveToPosition(int i) {
        LinearLayoutManager linlm = (LinearLayoutManager) getRecyclerView().getLayoutManager();
        int firstItem = 0;
        int lastItem = 0;
        if (linlm != null) {
            firstItem = linlm.findFirstVisibleItemPosition();
            lastItem = linlm.findLastVisibleItemPosition();
        }
        if (getItemCount() > 5) {//提前把9滑出来
            lastItem -= 3;
        }
        if (i <= firstItem) {
            getRecyclerView().scrollToPosition(i);
        } else if (i <= lastItem) {
            //当要置顶的项已经在屏幕上显示时不处理
        } else {
            //当要置顶的项在当前显示的最后一项的后面时
            getRecyclerView().scrollToPosition(i);
            int n = i - (linlm != null ? linlm.findFirstVisibleItemPosition() : 0);
            if (0 <= n && n < getRecyclerView().getChildCount()) {
                int top = getRecyclerView().getChildAt(n).getTop();
                getRecyclerView().smoothScrollBy(0, top);
            }
        }
    }
}
